<?php
namespace app\api\controller;
use think\Db;
use think\facade\{
    View,Env,Config
};
use EasyWeChat\Factory;
class Paynotice
{
    //微信通知
    public function wx(){
        $app = model('weixin')->getPayApp();
        $response = $app->handlePaidNotify(function($message, $fail){
        	
            $rs = Db::name('user_order')->where('order_no',$message['out_trade_no'])->find();
            if (!$rs) {
                $fail('Order not exist.');
            }elseif($rs['pay_status']!=0){
                return true;
            }
            if($message['result_code'] == 'SUCCESS'){
                model('userOrder')->complete($message['out_trade_no']);
            }
            return true;
        });
        return $response;
    }

    //商家续费:微信支付回调
    public function wxshoporder(){
        $app = model('weixin')->getPayApp();
        $response = $app->handlePaidNotify(function($message, $fail){
            $rs = Db::name('shop_order')->where('order_no',$message['out_trade_no'])->find();
            if (!$rs) {
                $fail('Order not exist.');
            }elseif($rs['pay_status']!=0){
                return true;
            }
            if($message['result_code'] == 'SUCCESS'){
                $pay_order_no = $message['transaction_id'];
                model('shopOrder')->complete($message['out_trade_no'],1, $pay_order_no);
            }
            return true;
        });
        return $response;
    }
}
